package edu.whu.rose.clock.ssql.util;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;

import edu.whu.rose.clock.kps.algorithm.blinks.index.Block;
import edu.whu.rose.clock.kps.graph.GraphMatrix;
import edu.whu.rose.clock.ssql.graph.RRadiusSubGraph;

public class SubgraphMatrix extends GraphMatrix {

	private int[] vertexes;
	
	public SubgraphMatrix(int[] vertexes, HashMap<Integer, HashSet<Integer>> edges) {
		super(vertexes.length);
		this.vertexes = vertexes;
		this.loadEdges(edges);
	}
	
	public SubgraphMatrix(RRadiusSubGraph rrsg) {
		super(rrsg.getVertexNumber());
		this.vertexes = rrsg.getVertexes();
		this.loadEdges(rrsg);
	}
	
	// for BLINKS
	public SubgraphMatrix(Block block) {
		super(block.size());
		this.vertexes = block.getVertexes();
		this.loadEdges(block);
	}
	
	// for BLINKS
	private void loadEdges(Block block) {
		this.fillDiagonal();
		
		int[] a = block.getStartVertexes();
		int[] b = block.getEndVertexes();
		
		for (int i = 0; i < a.length; i++) {
			insert(a[i], b[i]);
			insert(b[i], a[i]);
		}
	}
	
	private void loadEdges(RRadiusSubGraph rrsg) {
		this.fillDiagonal();
		
		int[] a = rrsg.getStartVertexes();
		int[] b = rrsg.getEndVertexes();
		
		for (int i = 0; i < a.length; i++) {
			insert(a[i], b[i]);
			insert(b[i], a[i]);
		}
	}
	
	private void loadEdges(HashMap<Integer, HashSet<Integer>> edges) {
		this.fillDiagonal();
		
		Iterator<Integer> it = edges.keySet().iterator();
		while (it.hasNext()) {
			Integer S = it.next();
			int source = S.intValue();
			HashSet<Integer> ends = edges.get(S);
			Iterator<Integer> it2 = ends.iterator();
			while (it2.hasNext()) {
				int end = it2.next().intValue();
				insert(source, end);
				insert(end, source);
			}
		}
	}
	
	public void fillDiagonal() {
		for (int i = 0; i < n; i++) {
			this.insert(vertexes[i], vertexes[i]);
		}
	}
	
	public int getVertex(int index) {
		return vertexes[index];
	}
}
